#!/bin/bash
set -e

# sector: 0   ?           2048            206848          411648
# image :     spl.bin     kernel.bin      romfs.img       ext2.img
# size  :       1MB         100MB           100MB          100MB

# write spl.bin
/home/zhangxu/study/minilinux/imxdownload /home/zhangxu/study/spl/spl.bin /dev/sdb
sync
sync
sync

# write kernel.bin
sudo dd if=/home/zhangxu/study/minilinux/kernel.bin of=/dev/sdb bs=512 seek=2048
sync
sync
sync

# copy and write romfs.img
for bin in $(find /home/zhangxu/study/minilinux/app -name "*.elf"); do
    echo ${bin}
    final_bin=${bin##*/}
    final_bin=${final_bin%%.*}
    echo ${final_bin}
    cp ${bin} /home/zhangxu/study/genromfs/filesystem/${final_bin}
    cp ${bin} /home/zhangxu/study/genromfs/filesystem/root/bin/${final_bin}
done
genromfs -d /home/zhangxu/study/genromfs/filesystem -f /home/zhangxu/study/genromfs/romfs.img -V zhangxu-romfs
sudo dd if=/home/zhangxu/study/genromfs/romfs.img of=/dev/sdb bs=512 seek=206848
sync
sync
sync

# write ext2.img
sudo dd if=/home/zhangxu/study/genext2/ext2.img of=/dev/sdb bs=512 seek=411648
sync
sync
sync

echo "make image done!"